# Makefile for Go project

# 变量定义
APP_NAME = your_app
MAIN_PATH = cmd/_your_app_/main.go
BUILD_DIR = build
DOCKER_IMAGE = $(APP_NAME):latest

# Go相关变量
GOCMD = go
GOBUILD = $(GOCMD) build
GOCLEAN = $(GOCMD) clean
GOTEST = $(GOCMD) test
GOGET = $(GOCMD) get
GOMOD = $(GOCMD) mod

# 默认目标
.PHONY: all
all: clean deps build

# 安装依赖
.PHONY: deps
deps:
	$(GOMOD) tidy
	$(GOMOD) download

# 构建应用
.PHONY: build
build:
	mkdir -p $(BUILD_DIR)
	$(GOBUILD) -o $(BUILD_DIR)/$(APP_NAME) $(MAIN_PATH)

# 构建Linux版本
.PHONY: build-linux
build-linux:
	mkdir -p $(BUILD_DIR)
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(BUILD_DIR)/$(APP_NAME)-linux $(MAIN_PATH)

# 构建Windows版本
.PHONY: build-windows
build-windows:
	mkdir -p $(BUILD_DIR)
	CGO_ENABLED=0 GOOS=windows GOARCH=amd64 $(GOBUILD) -o $(BUILD_DIR)/$(APP_NAME)-windows.exe $(MAIN_PATH)

# 构建macOS版本
.PHONY: build-darwin
build-darwin:
	mkdir -p $(BUILD_DIR)
	CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 $(GOBUILD) -o $(BUILD_DIR)/$(APP_NAME)-darwin $(MAIN_PATH)

# 构建所有平台版本
.PHONY: build-all
build-all: build-linux build-windows build-darwin

# 运行应用
.PHONY: run
run:
	$(GOCMD) run $(MAIN_PATH)

# 运行测试
.PHONY: test
test:
	$(GOTEST) -v ./...

# 运行测试并生成覆盖率报告
.PHONY: test-coverage
test-coverage:
	$(GOTEST) -v -coverprofile=coverage.out ./...
	$(GOCMD) tool cover -html=coverage.out -o coverage.html

# 清理构建文件
.PHONY: clean
clean:
	$(GOCLEAN)
	rm -rf $(BUILD_DIR)
	rm -f coverage.out coverage.html

# 格式化代码
.PHONY: fmt
fmt:
	$(GOCMD) fmt ./...

# 代码检查
.PHONY: vet
vet:
	$(GOCMD) vet ./...

# 安装开发工具
.PHONY: install-tools
install-tools:
	$(GOGET) -u golang.org/x/tools/cmd/goimports
	$(GOGET) -u github.com/golangci/golangci-lint/cmd/golangci-lint

# 代码质量检查
.PHONY: lint
lint:
	golangci-lint run

# 初始化项目环境
.PHONY: setup
setup:
	./scripts/setup.sh

# 初始化数据库
.PHONY: init-db
init-db:
	./scripts/init_database.sh

# Docker相关命令
.PHONY: docker-build
docker-build:
	docker build -t $(DOCKER_IMAGE) .

.PHONY: docker-run
docker-run:
	docker run -p 8080:8080 $(DOCKER_IMAGE)

.PHONY: docker-compose-up
docker-compose-up:
	docker-compose up -d

.PHONY: docker-compose-down
docker-compose-down:
	docker-compose down

# 显示帮助信息
.PHONY: help
help:
	@echo "可用的命令:"
	@echo "  deps              - 安装Go依赖"
	@echo "  build             - 构建应用"
	@echo "  build-linux       - 构建Linux版本"
	@echo "  build-windows     - 构建Windows版本"
	@echo "  build-darwin      - 构建macOS版本"
	@echo "  build-all         - 构建所有平台版本"
	@echo "  run               - 运行应用"
	@echo "  test              - 运行测试"
	@echo "  test-coverage     - 运行测试并生成覆盖率报告"
	@echo "  clean             - 清理构建文件"
	@echo "  fmt               - 格式化代码"
	@echo "  vet               - 代码检查"
	@echo "  lint              - 代码质量检查"
	@echo "  setup             - 初始化项目环境"
	@echo "  init-db           - 初始化数据库"
	@echo "  docker-build      - 构建Docker镜像"
	@echo "  docker-run        - 运行Docker容器"
	@echo "  docker-compose-up - 启动Docker Compose服务"
	@echo "  docker-compose-down - 停止Docker Compose服务"
	@echo "  help              - 显示帮助信息"

